package com.mf.controller.basic;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import com.mf.aspect.debounces.Debounce;
import com.mf.aspect.logApi.LogApi;
import com.mf.entity.dto.basic.DtoUser;
import com.mf.entity.req.basic.reqPublic.ReqRelevanceDetailById;
import com.mf.groups.GroupsAddInterface;
import com.mf.groups.GroupsUpdateInterface;
import com.mf.result.Result;
import com.mf.service.basic.ServiceRolePermission;
import com.mf.utils.UtilConverter;
import com.mf.utils.tree.UtilTree;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.mf.entity.dto.basic.DtoRole;
import com.mf.entity.req.basic.ReqRole;
import com.mf.service.basic.ServiceRole;
import com.mf.entity.req.basic.search.SearchReqRole;

import java.util.List;

/**
 * 角色表
 */
@RestController
@RequestMapping("role")
@Tag(name = "角色表")
@SaCheckLogin
@Debounce
public class ControllerRole {
    @Resource
    private ServiceRole servicerole;

    @Resource
    private ServiceRolePermission  serviceRolePermission;


    @Operation(summary = "分页列表查询树结构")
    @SaCheckPermission("role:search")
    @PostMapping("/page")
    public Result<Page<DtoRole>> findPage(@RequestBody SearchReqRole searchReqRole) {
        return Result.success(UtilTree.filterTree(servicerole.list(),servicerole.findPageTreeByParams(searchReqRole)));
    }

    @Operation(summary = "列表查询")
    @SaCheckPermission("role:search")
    @PostMapping("/list")
    public Result<List<DtoRole>> list(@RequestBody SearchReqRole searchReqRole) {
        return Result.success(UtilTree.filterTree(servicerole.list(),servicerole.findListByParams(searchReqRole)));
    }

    @Operation(summary = "保存")
    @SaCheckPermission("role:edit")
    @LogApi("角色保存")
    @PostMapping("/save")
    public Result<Void> save(@Validated(GroupsAddInterface.class) @RequestBody ReqRole reqRole) {
        reqRole.setUpdateUserId(StpUtil.getSession().getModel(SaSession.USER, DtoUser.class).getId());
        servicerole.save(UtilConverter.converter(reqRole, DtoRole.class));
        return Result.success();
    }

    @Operation(summary = "修改")
    @SaCheckPermission("role:edit")
    @LogApi("角色修改")
    @PostMapping("/update")
    public Result<Void> update(@Validated(GroupsUpdateInterface.class) @RequestBody ReqRole reqRole) {
        reqRole.setUpdateUserId(StpUtil.getSession().getModel(SaSession.USER, DtoUser.class).getId());
        servicerole.updateById(UtilConverter.converter(reqRole, DtoRole.class));
        return Result.success();
    }

    @Operation(summary = "根据角色id绑定权限")
    @SaCheckPermission("role:config")
    @LogApi("根据角色id绑定权限")
    @PostMapping("/savePremByRoleId")
    public Result<Void> savePremByRoleId(@Validated @RequestBody ReqRelevanceDetailById reqRelevanceDetailById) {
        serviceRolePermission.savePremByRoleId(reqRelevanceDetailById);
        return Result.success();
    }

    @Operation(summary = "根据角色id删除绑定权限")
    @SaCheckPermission("role:config")
    @LogApi("根据角色id删除绑定权限")
    @PostMapping("/delPremByRoleId")
    public Result<Void> delPremByRoleId(@Validated @RequestBody ReqRelevanceDetailById reqRelevanceDetailById) {
        serviceRolePermission.delPremByRoleId(reqRelevanceDetailById);
        return Result.success();
    }

    @Operation(summary = "删除")
    @SaCheckPermission("role:delete")
    @LogApi("删除权限")
    @GetMapping("/delete/{id}")
    public Result<Void> delete(@PathVariable("id") Integer id) {
        servicerole.deleteById(id);
        return Result.success();
    }

}
